package com.jpress.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;


@Entity
@Table(name="post")
@NamedQueries({
	@NamedQuery(name="Post.countPosts", query="SELECT COUNT(p) FROM Post p"),
	@NamedQuery(name="Post.findAllByCategoriaCodigo", query="SELECT p FROM Post p WHERE p.categoria.codigo = :categoriaCodigo ORDER BY p.codigo DESC"),
	@NamedQuery(name="Post.findAllByAutorCodigo", query="SELECT p FROM Post p WHERE p.autor.codigo = :autorCodigo ORDER BY p.codigo DESC"),
	@NamedQuery(name="Post.pesquisarByCategoriaCodigo", query="SELECT p FROM Post p WHERE p.titulo LIKE :titulo AND p.conteudo LIKE :conteudo AND p.categoria.codigo = :categoriaCodigo"),
	@NamedQuery(name="Post.pesquisarByAutorCodigo", query="SELECT p FROM Post p WHERE p.titulo LIKE :titulo AND p.conteudo LIKE :conteudo AND p.autor.codigo = :autorCodigo")
})
public class Post implements Serializable, Comparable<Post>{
	
	private static final long serialVersionUID = -8495980554681527938L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="codigo")
	private int codigo;
	
	@ManyToOne
	private Usuario autor;
	
	@ManyToOne
	private Categoria categoria;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="data", nullable=false)
	private Date data;
	
	@Column(name="titulo", nullable=false, length=200)
	private String titulo;
	
	@Column(name="conteudo", nullable=false, length=15000)
	private String conteudo;
	
	@Column(name="conteudoResumo", nullable=false, length=2000)
	private String conteudoResumo;
	
	public int getVoto() {
		return voto;
	}

	public void setVoto(int voto) {
		this.voto = voto;
	}

	@Column(name="voto", nullable=false)
	private int voto;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="post")
	private List<Comentario> comentarios;

	public Post(){}

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public Usuario getAutor() {
		return autor;
	}

	public void setAutor(Usuario autor) {
		this.autor = autor;
	}

	public String getNomeAutor(){
		return this.autor.getNome();
	}

	public Categoria getCategoria() {
		return categoria;
	}

	public void setCategoria(Categoria categoria) {
		this.categoria = categoria;
	}

	public Date getData() {
		return data;
	}

	public void setData(Date data) {
		this.data = data;
	}

	public String getTitulo() {
		return titulo;
	}

	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}

	public String getConteudo() {
		return conteudo;
	}

	public void setConteudo(String conteudo) {
		this.conteudo = conteudo;
	}

	public String getConteudoResumo() {
		return conteudoResumo;
	}

	public void setConteudoResumo(String conteudoResumo) {
		this.conteudoResumo = conteudoResumo;
	}

	public List<Comentario> getComentarios() {
		return comentarios;
	}

	public void setComentarios(List<Comentario> comentarios) {
		this.comentarios = comentarios;
	}

	public void adicionarComentario(Comentario comentario){
		this.comentarios.add(comentario);
	}
	
	@Override
	public boolean equals(Object object){
		return EqualsBuilder.reflectionEquals(this, object);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}
	
	@Override
	public int compareTo(Post o){
		return CompareToBuilder.reflectionCompare(this, o);
	}
}